import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart' hide Action;
import 'package:url_launcher/url_launcher.dart';
import 'action.dart';
import 'state.dart';
import '../../../app_files.dart';
import 'dart:io';

Effect<WebViewComponentState> buildEffect() {
  return combineEffects(<Object, Effect<WebViewComponentState>>{
    WebViewComponentAction.onBack: _onBack,
    WebViewComponentAction.onHandleUrl: _onHandleUrl,
  });
}

void _onBack(Action action, Context<WebViewComponentState> ctx) {}

void _onHandleUrl(Action action, Context<WebViewComponentState> ctx) async {

  String url = action.payload;
  if (url.startsWith('app://goback')) {
    Navigator.pop(ctx.context);
    bus.emit('webViewGoback');
    bus.emit('refreshUserInfo');
  }
  launch(url);
}